package io.gitee.chearnee.fw.rabbitmq;

import io.gitee.chearnee.fw.rabbitmq.annotation.EnableFWMq;
import io.gitee.chearnee.fw.rabbitmq.config.RabbitConfig;
import io.gitee.chearnee.fw.rabbitmq.enums.MqEnum;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotationMetadata;

import java.util.EnumMap;
import java.util.Map;
import java.util.function.Supplier;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 2023/2/16 16:14
 */
//@Configuration(proxyBeanMethods = false)
public class MqConfigurationInSelector implements ImportSelector, EnvironmentAware {

    private Environment environment;
    private static final EnumMap<MqEnum, Supplier<String>> map;

    static {
        map = new EnumMap<>(MqEnum.class);
        map.put(MqEnum.RABBIT, RabbitConfig.class::getName);
    }

    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        Map<String, Object> annotationAttributeMaps = importingClassMetadata.getAnnotationAttributes(EnableFWMq.class.getName());
        AnnotationAttributes annotationAttributes = AnnotationAttributes.fromMap(annotationAttributeMaps);
        assert annotationAttributes != null;
        MqEnum mqEnum = annotationAttributes.getEnum("value");
        return new String[]{map.get(mqEnum).get()};
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }
}
